package com.mk.admin.util;

import java.io.*;

public class IOTool {


    public static File createFile(String path) {
        File filePath = new File(path);
        if (!filePath.exists()) {
            filePath.mkdirs();//创建文件夹
        }
        return filePath;
    }

    /**
     * 通过路径和文件名得到文件
     * 没有路径则会创建路径
     *
     * @param parent   路径
     * @param fileName 文件名
     * @return
     */
    public static File file(String parent, String fileName) {
        createFile(parent);
        return new File(parent, fileName);
    }

    /**
     * 拷贝文件:字节流
     *
     * @param oldPath
     * @param newPath
     */
    public static void copyFile(String oldPath, String newPath) {
        File oldFile = new File(oldPath);
        File newFile = new File(newPath);
        try {
            InputStream inputStream = new FileInputStream(oldFile);
            OutputStream outputStream = new FileOutputStream(newFile);
            int read = 0;
            while ((read = inputStream.read()) != -1) {
                outputStream.write(read);
            }
            outputStream.flush();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
